//
//  LookLocationViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/6/25.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit

class LookLocationViewController: BaseViewController {

    var taskModel:PlatformTaskModel?
    
    @IBOutlet var mapView: MAMapView!
    
    var userLocation :MAUserLocation?
    var poiAnnotations :NSMutableArray?
    var point :MAPointAnnotation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "查看地址"
        initMapView()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if taskModel != nil {
            userLocation = MAUserLocation.init()
            poiAnnotations = NSMutableArray.init()
            point = MAPointAnnotation.init()
            point?.coordinate = CLLocationCoordinate2DMake((taskModel?.lat)!,(taskModel?.lng)!)
            poiAnnotations?.add(point!)
            showAnnotatios()
        }
    }
    
    func initMapView() -> Void {
        
        mapView.showsUserLocation = true
        mapView.showsCompass = false
        mapView.showsScale = false
        mapView.userTrackingMode = MAUserTrackingMode.follow
        mapView.delegate = self
        mapView.zoomLevel = 17
        mapView.setUserTrackingMode(MAUserTrackingMode.follow, animated: true)
    }
    
    func showAnnotatios() -> Void {
    
        self.mapView.removeAnnotations(mapView.annotations)
        poiAnnotations?.add(mapView.userLocation)
        self.mapView.addAnnotations(poiAnnotations as! [MAPointAnnotation])
        if poiAnnotations?.count == 1 {
            
            mapView.centerCoordinate = mapView.userLocation.coordinate
        }else {
            mapView.showAnnotations(poiAnnotations as! [Any], animated: true)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


extension LookLocationViewController:MAMapViewDelegate {
    
    func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
        return nil
    }
    
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        
        if annotation.isKind(of: MAUserLocation.self) {

             let userLocationStyleReuseIndetifier = "userLocationStyleReuseIndetifier"

            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: userLocationStyleReuseIndetifier)
            if (annotationView == nil){
                
                annotationView = MAPinAnnotationView.init(annotation: annotation, reuseIdentifier: "userLocationStyleReuseIndetifier")
                annotationView?.canShowCallout = true
            }
            

        }else if annotation.isKind(of: MAPointAnnotation.self) {
            
            var annotationView: MAPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier:"annotionView") as! MAPinAnnotationView?
            
            if annotationView == nil {
                annotationView = MAPinAnnotationView.init(annotation: annotation, reuseIdentifier: "annotionView")
            }
            annotationView!.image = UIImage.init(named: "homePage_wholeAnchor")
            annotationView!.canShowCallout = true
            annotationView!.isDraggable = false
            return annotationView
        }
       return nil
    }
    
    func mapView(_ mapView: MAMapView!, didUpdate userLocation: MAUserLocation!, updatingLocation: Bool) {
        
        
    }
    
}

